<?php
/**
 * 定时任务 - 店铺相关
 */
namespace Core\Controller;
use Think\Controller;
use  Common\Logic\GroupbuyLogic;

header("content-type:application/json");
set_time_limit(0);

class ShopTaskController extends Controller 
{
	/**
	 * 店铺订单进行结算
	 * 每一分钟结算一次
	 * https://dstest.pxuc.com.cn/index.php/Core/ShopTask/shop_order_settlement
	 */
    public function shop_order_settlement() 
    {
		// 查询已经结算的订单
		$where_order['is_settlement'] = 0;
		$where_order['pay_status'] = 1;
		$where_order['order_status'] = array('in', array(2,4));
		$order = M('order')->where($where_order)->limit(5)->select();
		foreach($order as $k => $v){
			// 结算给商户
			M()->startTrans(); // 开启事务
            $desc = "订单定时任务主动结算";
            // 添加结算记录
            $plant_order_amount = 0;//订单平台占比
            $order_rate = tpCache('basic.order_rate');

            if($order_rate > 0){
                $plant_order_amount = bcmul($v['order_amount'],bcdiv($order_rate, 100, 2), 2);
                $desc .= ',平台占比'.$order_rate.'%';
            }
			//同城运费结算
            $shipping_price_shop = 0;
			if($v['shipping_code'] == 'dada'){
			    //平台分担比例
			    $dd_exp_fee_rate = $v['exp_fee_rate'];
                $desc .= '，快递费用平台占比'.$dd_exp_fee_rate.'%';
                if($dd_exp_fee_rate > 0){
                    $dd_exp_fee_rate = $dd_exp_fee_rate > 100 ? 100 : $dd_exp_fee_rate;
                    $shipping_price_sub = bcsub($v['shipping_price_real'], $v['shipping_price'], 2); //平台，店铺分担运费费用
                    if($shipping_price_sub > 0){
                        //平台分担
                        $shipping_price_plant = bcmul($shipping_price_sub,bcdiv($dd_exp_fee_rate, 100, 2), 2);
                        //店铺分担
                        $shipping_price_shop = bcsub($shipping_price_sub, $shipping_price_plant,2);
                    }else{
                        $shipping_price_plant = 0;
                    }
                }else if($dd_exp_fee_rate == 0){
                    $shipping_price_plant = 0;
                    $shipping_price_shop = bcsub($v['shipping_price_real'], $v['shipping_price'], 2);
                    $shipping_price_shop = $shipping_price_shop > 0 ? $shipping_price_shop : 0;
                }else{
                    $shipping_price_plant = bcsub($v['shipping_price_real'], $v['shipping_price'], 2);
                }

                $add_shop_capital_log['exp_money'] = $v['shipping_price_real'];
                $add_shop_capital_log['exp_money_user'] = $v['shipping_price'];
                $add_shop_capital_log['exp_money_shop'] = $shipping_price_shop;
                $add_shop_capital_log['exp_money_plant'] = $shipping_price_plant;
            }


            //商户实际金额
            $change_money = bcsub($v['order_amount'], $plant_order_amount, 2);
            $change_money = bcadd($change_money, $v['shipping_price'], 2);
            $change_money = bcsub($change_money, $shipping_price_shop, 2);

            $add_shop_capital_log['shop_id'] = $v['shop_id'];
            $add_shop_capital_log['order_id'] = $v['order_id'];
            $add_shop_capital_log['change_money'] = $change_money;
            $add_shop_capital_log['service_money'] = $plant_order_amount;
            $add_shop_capital_log['desc'] = $desc;
            $add_shop_capital_log['create_time'] = \time();
            $add_shop_capital_log['update_time'] = \time();
            if(!M('shop_capital_log')->add($add_shop_capital_log)){
                M()->rollback(); // 事务回滚
            }

			// 修改订单结算状态
			if(!M('order')->where(['order_id'=>$v['order_id']])->save(['is_settlement'=>1])){
				M()->rollback(); // 事务回滚
			}

			$account_info = accountLogShop($v['shop_id'], $change_money, '入账', 1, $plant_order_amount + $shipping_price_shop, 0, $v['order_id'], $v['order_sn']);
			// 给商户结算
			if(!$account_info['is_success']){
				M()->rollback(); // 事务回滚
			}
			M()->commit(); // 事务提交
			echo $v['order_sn']."订单结算成功".PHP_EOL;
		}
		
	}
	
	/**
	 * 每天给结算商户钱
	 * 每小时一次
	 * https://dstest.pxuc.com.cn/index.php/Core/ShopTask/settlement_wallet
	 */
	public function settlement_wallet(){
		// 每周一结算金额
		if(date("w") == 1){
			// 查询有100余额的用户
			$where_shop['wallet_balance'] = ['GT',100];
			$shop_list = M('shop')->where($where_shop)->select();
			
			foreach($shop_list as $k => $v){
				// 检测今天有没有结算
				$shop_settlement_log = M('shop_settlement_log')->where(['shop_id'=>$v['id']])->find();
				if(!$shop_settlement_log){
					// 结算金额
					M()->startTrans(); // 开启事务
					if(!M('shop')->where(['id'=>$v['id']])->setDec('wallet_balance',$v['wallet_balance'])){
						M()->rollback(); // 事务回滚
					}
					$add_log['shop_id'] = $v['id'];
					$add_log['change_money'] = $v['wallet_balance'];
					$add_log['add_time'] = time();
					$add_log['last_time'] = time();
					$add_log['is_settlement'] = 0;
					if(!M('shop_settlement_log')->add($add_log)){
						M()->rollback(); // 事务回滚
					}
					M()->commit(); // 事务提交
					echo $v['id']."商户结算成功".PHP_EOL;
				}
			}
		}
	}
	
}